#include <iostream>
#include <memory>
#include <string>
#include "server_tcp.hpp"
#include "log.hpp"
#include "error.hpp"
#include "Ulit.hpp"

const std::string SEP = "\r\n";
const std::string wetRoot = "./wwwroot";
const std::string defaultHomePage = "index.html";


std::string HttpRespon(const std::string &req)
{
    std::cout << req << std::endl;

    std::string path = wetRoot + "/" + "军训_最终版_4V.7z";
    std::string body;
    if (!Ulit::ReadFile(path, &body))
    {
        logMessage(Error, "ReadFile error");
        return "";
    }
    // 构造响应
    // 1. 响应行
    std::string res = "HTTP/1.0 301 Moved Permanently" + SEP;
    // 2. 响应头部
    res += "Content-Type: text/html" + SEP;
    res += "Content-Length: "+std::to_string(body.size()) + SEP;
    res += "Connection: keep-alive" + SEP;
    res += "Location: https://www.baidu.com/" + SEP;
    res += SEP;
    // 3. 响应正文
    res += body;
    return res;
}

int main()
{
    std::unique_ptr<ServerTCP> server(new ServerTCP(HttpRespon));
    server->Init();
    server->Start();
    return 0;
}